package com.lemon.smartsafetynew.ui.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafetynew.ui.activity.base.BaseActivity;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafetynew.entity.event.user.PageEvent;
import com.lemon.smartsafetynew.ui.activity.MainNewActivity;
import com.lemon.smartsafetynew.ui.fragment.user.register.UserRegisterStepOneFragment;
import com.lemon.smartsafetynew.ui.fragment.user.register.UserRegisterStepThreeFragment;
import com.lemon.smartsafetynew.ui.fragment.user.register.UserRegisterStepTwoFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/7/17.
 */

public class UserRegisterActivity extends BaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.root_layout)
    LinearLayout rootLayout;

    private static int CURRENT_PAGE = 1; //当前页

    private BaseFragment stepOneFragment;
    private BaseFragment stepTwoFragment;
    private BaseFragment stepThreeFragment;

    @Override
    protected void getBundleArgs() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_toolbar_fragment;
    }

    @Override
    protected void initViews() {

        toolbar.setTitle("");
        mainTitle.setText(getString(R.string.user_register));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backPage();
            }
        });

        CURRENT_PAGE = 1;
        stepOneFragment = new UserRegisterStepOneFragment();
        transactionSupportFragment(stepOneFragment);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(PageEvent event) {
        forwardPage();
    }

    private void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onBackPressed() {
        backPage();
    }

    private void backPage() {
        Bundle bundle;
        CURRENT_PAGE--;
        switch (CURRENT_PAGE) {
            case 0:
                callFinish();
                break;
            case 1:
                if (stepOneFragment == null) {
                    stepOneFragment = new UserRegisterStepOneFragment();
                }
                transactionSupportFragment(stepOneFragment);
                break;
            case 2:
                if (stepTwoFragment == null) {
                    stepTwoFragment = new UserRegisterStepTwoFragment();
                }
                transactionSupportFragment(stepTwoFragment);
                break;

        }
    }

    private void forwardPage() {
        CURRENT_PAGE++;
        switch (CURRENT_PAGE) {
            case 2:
                if (stepTwoFragment == null) {
                    stepTwoFragment = new UserRegisterStepTwoFragment();
                }
                transactionSupportFragment(stepTwoFragment);
                break;
            case 3:
                if (stepThreeFragment == null) {
                    stepThreeFragment = new UserRegisterStepThreeFragment();
                }
                transactionSupportFragment(stepThreeFragment);
                break;
            case 4:
                Intent mIntent = new Intent(UserRegisterActivity.this, MainNewActivity.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(mIntent);
                break;

        }
    }

    private void transactionSupportFragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fragment).commit();

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(rootLayout.getWindowToken(), 0); //强制隐藏键盘
    }

}
